package com.hengxinguotong.zhihuichengjian.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Authority;
import com.hengxinguotong.zhihuichengjian.ui.engineering.reservation.ReservationListActivity;
import com.hengxinguotong.zhihuichengjian.ui.engineering.task.TaskListActivity;
import com.hengxinguotong.zhihuichengjian.ui.quality.RectificationListActivity;
import com.hengxinguotong.zhihuichengjian.utils.ActivityController;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.view.CustomPatient;
import com.hengxinguotong.zhihuichengjian.widget.view.MyDynamicBox;
import com.hengxinguotong.zhihuichengjian.widget.view.StatusBarCompat;
import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import cn.jpush.android.api.JPushInterface;

/**
 * 基类
 */
public class BaseActivity extends AppCompatActivity {
    protected Authority authority;
    protected MyDynamicBox myDynamicBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
        StatusBarCompat.compat(this, getResources().getColor(R.color.main_color));//设置手机状态栏背景颜色
        authority = (Authority) getIntent().getSerializableExtra("authority");
        JAnalyticsInterface.onPageStart(this,this.getClass().getCanonicalName());
    }

    @Override
    protected void onStart() {
        super.onStart();
        ActivityController.setCurrentActivity(this);
    }

    protected void showEmptyView(View view) {
        if(myDynamicBox==null){
            myDynamicBox = new MyDynamicBox(this, view);
        }
        myDynamicBox.showCustomView(MyDynamicBox.EMPTY);
    }


    protected void dismissEmptyView() {
        if(myDynamicBox!=null){
            myDynamicBox.hideAll();
        }
    }

    @Override
    protected void onResume() {

        super.onResume();
        JPushInterface.onResume(getApplicationContext());
    }
    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(getApplicationContext());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JAnalyticsInterface.onPageEnd(this,this.getClass().getCanonicalName());
        ActivityController.removeActivity(this);
    }

    public void showToast(String toastString) {
        Toast.makeText(this, toastString, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int toastId) {
        Toast.makeText(this, toastId, Toast.LENGTH_SHORT).show();
    }

    protected boolean hasSubmit() {
        if (authority == null) {
            return false;
        }
        if (authority.getSubmit() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasAction() {
        if (authority == null) {
            return false;
        }
        if (authority.getAction() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasDelete() {
        if (authority == null) {
            return false;
        }
        if (authority.getDelete() == 1) {
            return true;
        }
        return false;
    }


    protected boolean hasEdit() {
        if (authority == null) {
            return false;
        }
        if (authority.getEdit() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasAudit() {
        if (authority == null) {
            return false;
        }
        if (authority.getAudit() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasList() {
        if (authority == null) {
            return false;
        }
        if (authority.getList() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasAdd() {
        if (authority == null) {
            return false;
        }
        if (authority.getAdd() == 1) {
            return true;
        }
        return false;
    }


    protected boolean hasPrint() {
        if (authority == null) {
            return false;
        }
        if (authority.getPrint() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasUpdate() {
        if (authority == null) {
            return false;
        }
        if (authority.getUpdate() == 1) {
            return true;
        }
        return false;
    }

    protected boolean hasAffirm() {
        if (authority == null) {
            return false;
        }
        if (authority.getAffirm() == 1) {
            return true;
        }
        return false;
    }

    public void showPopwindow(String title, String message, String map){
        try {
            JSONObject json = new JSONObject(map);
            final int type = json.getInt("type");//消息类型：0.系统消息； 1.质量整改； 2.安全整改； 3.任务派发； 4.塔吊预约； 5.升降机预约
            final int status = json.getInt("status");//1:待受理; 2:处理中;3:待检查;4:检查不通过；5:已完成
            CustomPatient.Builder builder = new CustomPatient.Builder(this);
            Typeface lantypeFace = Utils.getTypeface(this);
            Typeface typeFace = Utils.getTypeface(this);
            builder.setTitle(title);
            builder.setContent(message, lantypeFace, typeFace);
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    Intent intent = null;
                    switch (type){//消息类型：0.系统消息； 1.质量整改； 2.安全整改； 3.任务派发； 4.塔吊预约； 5.升降机预约
                        case 1:
                            intent = new Intent(BaseActivity.this, RectificationListActivity.class);
                            intent.putExtra("type", Constants.QUALITY);
                            intent.putExtra("status", status);
                            break;
                        case 2:
                            intent = new Intent(BaseActivity.this, RectificationListActivity.class);
                            intent.putExtra("type", Constants.SECURITY);
                            intent.putExtra("status", status);
                            break;
                        case 3:
                            intent = new Intent(BaseActivity.this, TaskListActivity.class);
                            intent.putExtra("status", status);
                            break;
                        case 4:
                            intent = new Intent(BaseActivity.this, ReservationListActivity.class);
                            intent.putExtra("equipmentType", 1);
                            break;
                        case 5:
                            intent = new Intent(BaseActivity.this, ReservationListActivity.class);
                            intent.putExtra("type", 2);
                            break;
                        default:

                            break;
                    }
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
            });

            builder.setNegativeButton("取消",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            CustomPatient CustomPatient = builder.create();
            CustomPatient.show();
            Window window = CustomPatient.getWindow() ;
            WindowManager m = getWindowManager();
            Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
            WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
            p.height = (int) (d.getHeight() * 0.23); // 改变的是dialog框在屏幕中的位置而不是大小
            p.width = (int) (d.getWidth() * 0.85); // 宽度设置为屏幕的0.65
            window.setAttributes(p);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }

}
